package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.FOrder;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
/*云仓订单发货单*/
public class QuerryYCRetailout extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public QuerryYCRetailout(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<FOrder> getData(String ycRetailId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(ycRetailId), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        ArrayList<FOrder> orderArrayList = new ArrayList<FOrder>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);

            FOrder forder = new FOrder();

            forder.setId(jsonArray.getJSONArray(i).getString(0));
            forder.setcExpresscompanyId(jsonArray.getJSONArray(i).getString(1));
            forder.setExpresscompanyNo(jsonArray.getJSONArray(i).getString(2));
            forder.setStaus(jsonArray.getJSONArray(i).getString(3));
            forder.setIsUp(jsonArray.getJSONArray(i).getString(4));

            orderArrayList.add(forder);
        }
        return orderArrayList;
    }

    private MultiValueMap Data(String ycRetailId) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");

        columns.add("'C_EXPRESSCOMPANY_ID;NAME'");//快递公司 EB_LOGIS_ID  FASTNO
        columns.add("EXPRESSCOMPANY_NO");//快递单号
        columns.add("STATUS");//快递单号
        columns.add("IS_UP");


        params.put("columns", columns);
        params.put("table", "YC_RETAILOUT");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号
        String ac = "'="+ycRetailId+"'";
        paramss.put("column", "'YC_RETAIL_ID;DOCNO'");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QuerryYCRetailout querryYCRetailout = new QuerryYCRetailout("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(querryYCRetailout.getData("YCRE21032100005"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
